#encoding:UTF-8
import urllib2
import mylib
import re
from datetime import date
from time import mktime
import pythonlibSB



def get_content(url):
    try:
        respone = urllib2.urlopen(url)
        content = respone.read()
        return content
    except:
        return ''
    
def get_date_today(content):
    try:
        current_time = mylib.get_timeunix_today()      
        for i in re.finditer('([0-9]+)[/]([0-9]+)[/]([0-9]+)',content):
            ngay = int(i.group(1)[0:2])
            print ngay
            thang = int(i.group(2)[0:2])
            print thang
            nam = int(i.group(3))
            print nam
            thistime = date(nam,thang,ngay)
            this_unixtime = mktime(thistime.timetuple())
            print current_time
            print this_unixtime
            if current_time == this_unixtime:
                print "ko vao day"
                str_time = str(ngay)+'/'+str(thang)+'/'+str(nam)
                print str_time
                return str_time
    except:
        return ''

def get_url_today(content):
    try:
        pos1 = content.find('id="list_categories"')
        pos2 = content.find('id="entries"',pos1)
        tmp = content[pos1:pos2]
        today_str = get_date_today(tmp)
        print tmp
        print today_str
        import pdb
        pdb.set_trace()
        str_container = pythonlibSB.s_string(tmp,today_str,'a')
        reg = re.compile('(href=")(.*?)(")')
        if reg.search(str_container):
            url_today = str(reg.search(str_container).group(2))
            print url_today
            return url_today
    except:
        return ''
    

if __name__ == "__main__":
    content = get_content('http://www.boihoroscope.com/chuyen-muc/campus-horoscope-tu-vi-hang-ngay/')
    today = get_url_today(content)
    print today